package com.meepo.framework.design.text.icontext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.meepo.framework.R;
import com.meepo.framework.design.helper.Utils;

public class IconText extends RelativeLayout {


    private int mSize;

    private String mTitle;
    @DrawableRes
    private int mIconResId;

    private int mTitleColor;

    private float mIconSizeRate;

    private final Context mContext;

    public IconText(Context context) {
        this(context, null);
    }

    public IconText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

//    public IconText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//        this(context, attrs, defStyleAttr, 0);
//    }

    public IconText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IconText);

        mSize = ta.getDimensionPixelSize(R.styleable.IconText_it_size, Utils.dp2px(context, 60));
        mTitle = ta.getString(R.styleable.IconText_it_title);

        mIconResId = ta.getResourceId(R.styleable.IconText_it_iconResId, 0);
        mTitleColor = ta.getColor(R.styleable.IconText_it_titleColor, 0);
        mIconSizeRate = ta.getFloat(R.styleable.IconText_it_iconSizeRate, 0.4f);

        ta.recycle();


//        this.setLayoutParams(new RelativeLayout.LayoutParams(mSize, mSize));
        notifyAttrsChanged();
    }


    private void notifyAttrsChanged() {

        this.removeAllViews();

        View view = View.inflate(mContext, R.layout.icon_text_layout, null);
        view.setLayoutParams(new RelativeLayout.LayoutParams(mSize, mSize));

        if (mIconResId != 0) {

            int iconSize = (int) (mSize * mIconSizeRate);

            ImageView icon = view.findViewById(R.id.icon_iv);
            ViewGroup.LayoutParams layoutParams = icon.getLayoutParams();

            layoutParams.height = iconSize;
            layoutParams.width = iconSize;

            icon.setLayoutParams(layoutParams);

            Drawable iconDrawable = ResourcesCompat.getDrawable(mContext.getResources(), mIconResId, mContext.getTheme());
            icon.setImageDrawable(iconDrawable);
        }


        float titleSize = mSize / 16.0f;

        TextView title = view.findViewById(R.id.title_tv);
        title.setTextSize(titleSize);
        title.setMaxWidth(mSize);
        title.setText(mTitle);
        if (mTitleColor != 0) {
            title.setTextColor(mTitleColor);
        }

        addView(view);
    }

    public int getSize() {
        return mSize;
    }

    public void setSize(int mSize) {
        this.mSize = mSize;
        notifyAttrsChanged();
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
        notifyAttrsChanged();

    }

    public int getIconResId() {
        return mIconResId;
    }

    public void setIconResId(int mIconResId) {
        this.mIconResId = mIconResId;
        notifyAttrsChanged();

    }

    public int getTitleColor() {
        return mTitleColor;
    }

    public void setTitleColor(int mTitleColor) {
        this.mTitleColor = mTitleColor;
        notifyAttrsChanged();

    }
}
